package com.nakamachizu.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slim3.util.StringUtil;

public class PocketCloudStringUtil {

    public static String htmlSpecialChars(String str){
        String result = null;
        if(str != null){
            // 元の文字列は変更しない
            result = new String(str);
            result = result.replaceAll("&", "&amp;");
            result = result.replaceAll("<", "&lt;");
            result = result.replaceAll(">", "&gt;");
            result = result.replaceAll("\"", "&quot;");
            result = result.replaceAll("'", "&#039;");
        }
        return result;
    }
    
    public static String removeReturn(String str){
        if(!StringUtil.isEmpty(str)){
            str = str.replaceAll("[\r\n]", "");
        }
        return str;
    }
    
    public static String convertWindows31J(String str){
        String result;
        result = str.replaceAll("\u301C", "\uFF5E");

        return result;
    }
    
    public static List<String> getHttpHeaders(HttpServletRequest request){
        List<String>result = new ArrayList<String>();
        Enumeration names = request.getHeaderNames();
        while(names.hasMoreElements()){
            String name = (String)names.nextElement();
            result.add(name + ": " + request.getHeader(name));
        }

        return result;
    }
    
    public static String bridgeNull(String str){
        String result = null;
        if(str == null){
            result = "";
        }else{
            result = new String(str);
        }
        return result;
    }
    
    public static String makeDoubleUrlEncode(String str, String charSet) throws UnsupportedEncodingException{
        String result = null;
        if(str == null){
            result = "";
        }else{
            result = new String(str);
            result = URLEncoder.encode(result, charSet);
            result = URLEncoder.encode(result, charSet);
        }
        
        return result;
    }

}
